#include <iostream>
#include <string>
#include "test_common.h"
#include "windows.h"


using namespace std;

void dumpFftResultToFile(float * fftData, int len, const char * strFileName)
{
	FILE * fp = fopen(strFileName, "w");

	char strToWrite[1024];
	strToWrite[0] = '\0';
	for (int i = 0; i < len; i++)
	{
		if (fftData[i + len] >= 0)
		{
			sprintf(strToWrite, "%s %f + %fi", strToWrite, fftData[i], fftData[i + len]);
		}
		else
		{
			sprintf(strToWrite, "%s %f %fi", strToWrite, fftData[i], fftData[i + len]);
		}
		
		if (i % 20 == 19)
		{
			sprintf(strToWrite, "%s\n", strToWrite);
		}
		if (strlen(strToWrite) >= 900)
		{
			fwrite(strToWrite, strlen(strToWrite), 1, fp);
			memset(strToWrite, 0, 1024);
		}
	}
	if (strlen(strToWrite))
	{
		fwrite(strToWrite, strlen(strToWrite), 1, fp);
	}

	fclose(fp);
}

double getCpuClock(void)
{
	LARGE_INTEGER freq;
	LARGE_INTEGER count;
	QueryPerformanceFrequency(&freq);

	QueryPerformanceCounter(&count);

	//cout << "freq:" << freq.QuadPart << ",count:" << count.QuadPart << endl;

	return ((double)count.QuadPart) / ((double)freq.QuadPart);
}


void dumpComplexDataArrayToFile(Complex_local *dataIn, int len, char*strFileName)
{
	FILE * fp = fopen(strFileName, "w");

	char strToWrite[1024];
	strToWrite[0] = '\0';
	for (int i = 0; i < len; i++)
	{
		if (dataIn[i].y >= 0)
		{
			sprintf(strToWrite, "%s %f + %fi", strToWrite, dataIn[i].x, dataIn[i].y);
		}
		else
		{
			sprintf(strToWrite, "%s %f %fi", strToWrite, dataIn[i].x, dataIn[i].y);
		}

		if (i % 20 == 19)
		{
			sprintf(strToWrite, "%s\n", strToWrite);
		}
		if (strlen(strToWrite) >= 900)
		{
			fwrite(strToWrite, strlen(strToWrite), 1, fp);
			memset(strToWrite, 0, 1024);
		}
	}
	if (strlen(strToWrite))
	{
		fwrite(strToWrite, strlen(strToWrite), 1, fp);
	}

	fclose(fp);
}

void dumpComplexDataMatrixToFile(Complex_local *dataIn, int height, int width, const char*strFileName)
{
	FILE * fp = fopen(strFileName, "w");

	char strToWrite[1024];
	strToWrite[0] = '\0';
	int len = width*height;
	for (int i = 0; i < len; i++)
	{
		if ((i>0)&&(i % width == 0))
		{
			sprintf(strToWrite, "%s\n", strToWrite);
		}
		if (dataIn[i].y >= 0)
		{
			sprintf(strToWrite, "%s %f + %fi", strToWrite, dataIn[i].x, dataIn[i].y);
		}
		else
		{
			sprintf(strToWrite, "%s %f %fi", strToWrite, dataIn[i].x, dataIn[i].y);
		}

		if (i % 20 == 19)
		{
			sprintf(strToWrite, "%s\n", strToWrite);
		}
		if (strlen(strToWrite) >= 900)
		{
			fwrite(strToWrite, strlen(strToWrite), 1, fp);
			memset(strToWrite, 0, 1024);
		}
	}
	if (strlen(strToWrite))
	{
		fwrite(strToWrite, strlen(strToWrite), 1, fp);
	}

	fclose(fp);
}